package com.yeswater.redis7.service;

import cn.hutool.core.date.DateUtil;
import com.yeswater.redis7.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author： yeswater
 * @create： 2024/6/28
 */

@Service
@Slf4j
public class JHSTaskService {
	public static final String JHS_KEY = "jhs";
	public static final String JHS_KEY_A = "jhs:a";
	public static final String JHS_KEY_B = "jhs:b";
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	/**
	 * 偷个懒不加mybatis了，模拟从数据库读取100件特价商品，用于加载到聚划算的页面中
	 *
	 * @return
	 */
	private List<Product> getProductsFromMysql() {
		List<Product> list = new ArrayList<>();
		for (int i = 1; i <= 20; i++) {
			Random rand = new Random();
			int id = rand.nextInt(10000);
			Product obj = new Product((long) id, "product" + i, i, "detail");
			list.add(obj);
		}
		return list;
	}
	
	//@PostConstruct
	//public void initJHS() {
	//	log.info("启动定时器淘宝聚划算功能模拟..........{}", DateUtil.now());
	//	new Thread(() -> {
	//		//模拟定时器一个后台任务，定时把数据库的特价商品，刷新到redis中
	//		while (true) {
	//			//模拟从数据库读取100件特价商品，用于加载到聚划算的页面中
	//			List<Product> list = this.getProductsFromMysql();
	//			//采用redis list数据结构的lpush来实现存储
	//			this.redisTemplate.delete(JHS_KEY);
	//			//lpush命令
	//			this.redisTemplate.opsForList().leftPushAll(JHS_KEY, list);
	//			//间隔一分钟 执行一遍，模拟聚划算每3天刷新一批次参加活动
	//			try {
	//				TimeUnit.MINUTES.sleep(1);
	//			} catch (InterruptedException e) {
	//				e.printStackTrace();
	//			}
	//			
	//			log.info("runJhs定时刷新..............");
	//		}
	//	}, "t1").start();
	//}
	
	@PostConstruct
	public void initJHSAB() {
		log.info("启动AB定时器计划任务淘宝聚划算功能模拟..........{}", DateUtil.now());
		new Thread(() -> {
			//模拟定时器，定时把数据库的特价商品，刷新到redis中
			while (true) {
				//模拟从数据库读取100件特价商品，用于加载到聚划算的页面中
				List<Product> list = this.getProductsFromMysql();
				//先更新B缓存
				this.redisTemplate.delete(JHS_KEY_B);
				this.redisTemplate.opsForList().leftPushAll(JHS_KEY_B, list);
				this.redisTemplate.expire(JHS_KEY_B, 20L, TimeUnit.DAYS);
				//再更新A缓存
				this.redisTemplate.delete(JHS_KEY_A);
				this.redisTemplate.opsForList().leftPushAll(JHS_KEY_A, list);
				this.redisTemplate.expire(JHS_KEY_A, 15L, TimeUnit.DAYS);
				//间隔一分钟 执行一遍
				try {
					TimeUnit.MINUTES.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				log.info("runJhs定时刷新双缓存AB两层..............");
			}
		}, "t1").start();
	}
}
